package cn.edu.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author Davey chan
 */
//通知类

@Component
@Aspect
public class MyAdvice {
//    定义一个切入点
//    这里去匹配bookDao 对象，匹配成功就生成代理对象，反之就是创建对象
    @Pointcut("execution(void cn.edu.dao.BookDao.update())")
    private void pt(){}

    @Pointcut("execution(int cn.edu.dao.BookDao.select())")
    private void pt2(){}
/*绑定切入点与通知的关系
    获取的bean是代理对象根据代理对象的运行模式进行增强
    @Before("pt()")
    public  void method(){
        System.out.println(System.currentTimeMillis());
    }*/

/*    通知类类型
    @Before("pt()")
    public void before(){
        System.out.println("before...");
    }*/
/*  @After("pt()")
    public void after(){
        System.out.println("after...");
    }*/
  /*  @Around("pt()")
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before around..");
        pjp.proceed();
        System.out.println("after around...");
    }*/
@Around("pt2()")//这里的切入点有返回值是int类型，需要给返回
public Object around(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("before around..");
//    Integer proceed = (Integer) pjp.proceed();
    Object proceed = pjp.proceed();
    System.out.println("after around...");
    return proceed;
}

     @AfterReturning("pt()")//成功运行之后执行
    public void afterReturning(){
        System.out.println("afterRetuning advice...");

    }
    @AfterThrowing("pt()")//有异常就抛出
    public void AfterThrowing(){
        System.out.println("afterThrowing advice...");
    }
}
